package tum0r.model;

import tum0r.util.StringUtils;

import java.io.*;
import java.text.NumberFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.UUID;

/**
 * 工程: ResourcesDatabase<br>
 * 包: tum0r.model<br>
 * 创建者: tum0r<br>
 * 创建时间: 2020/10/4 17:17<br>
 * <br>
 */
public class Resource {
    public String ID;
    public String Name;
    public String Type;
    public String Label;
    public Date CreateTime;
    public Date LastAccessTime;
    public long Size;

    public String sizeConvert;
    public File resource;
    public LinkedList<ResourceContent> resourceContent;

    public void setNewID() {
        this.ID = UUID.randomUUID().toString().replace("-", "");
    }

    public void setResource(File resource) {
        if (resource != null && resource.exists() && resource.isFile() && resource.canRead()) {
            this.resource = resource;
            String[] fileNameInfo = resource.getName().split("\\.");
            this.Name = fileNameInfo[0];
            int count = fileNameInfo.length - 1;
            for (int j = 1; j < count; j++) {
                this.Name = this.Name.concat(".").concat(fileNameInfo[j]);
            }
            this.Type = fileNameInfo.length >= 2 ? fileNameInfo[fileNameInfo.length - 1].toLowerCase() : "";
            this.CreateTime = new Date();
            this.LastAccessTime = new Date(resource.lastModified());
            this.Size = resource.length();

            if (resourceContent == null) {
                resourceContent = new LinkedList<>();
            } else {
                resourceContent.clear();
            }
            try {
                FileInputStream fileInputStream = new FileInputStream(resource);
                byte[] data = new byte[StaticResource.FILE_MAX_SIZE];
                byte[] temp;
                int index = 0;
                while ((count = fileInputStream.read(data)) != -1) {
                    temp = new byte[count];
                    for (int i = 0; i < count; i++) {
                        temp[i] = data[i];
                    }
                    ResourceContent content = new ResourceContent();
                    content.setNewID();
                    content.setContent(temp);
                    content.setFingerprint();
                    content.Index = index++;
                    resourceContent.addLast(content);
                }
                fileInputStream.close();
            } catch (IOException ignored) {
            }
        }
    }

    public boolean saveFile(String path) {
        boolean result = false;
        if (!StringUtils.isNullOrOnlyWhiteSpace(path) && resourceContent != null && resourceContent.size() != 0) {
            try {
                FileOutputStream fileOutputStream = new FileOutputStream(path);
                for (ResourceContent content : resourceContent) {
                    if (content != null && content.Content != null) {
                        fileOutputStream.write(content.Content);
                    }
                }
                fileOutputStream.close();
                result = true;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    public void convertSize() {
        double B = 1.0;
        double KB = B * 1024;
        double MB = KB * 1024;
        double GB = MB * 1024;
        NumberFormat numberFormat = NumberFormat.getNumberInstance();
        numberFormat.setMaximumFractionDigits(2);

        if (Size >= GB) {
            sizeConvert = numberFormat.format(Size / GB) + " GB";
        } else if (Size >= MB) {
            sizeConvert = numberFormat.format(Size / MB) + " MB";
        } else if (Size >= KB) {
            sizeConvert = numberFormat.format(Size / KB) + " KB";
        } else {
            sizeConvert = numberFormat.format(Size / B) + " B";
        }
    }
}
